package br.com.esporte.view.site.component.cartao;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;

import br.com.esporte.model.TipoAdvertenciaEnum;
import br.com.esporte.view.infra.component.ComponenteGenerico;
import br.com.esporte.view.infra.html.HtmlParam;
import br.com.esporte.view.infra.html.JavaScriptParam;

/**
 * 
 * @author Milton Domingues
 *
 */
public class CartaoComponent extends ComponenteGenerico {

	private static final long serialVersionUID = 1457163771734250911L;

	private static final String CARTAO_ID = "cartao";
	private static final String STYLE_CARTAO = "width: 20px; height: 20px; float: left; margin-left: 10px; cursor: pointer;";

	private TipoAdvertenciaEnum advertencia;

	private boolean advertido;
	private Form<Component> form;

	public CartaoComponent(String id, Form<Component> form, TipoAdvertenciaEnum advertencia) {
		this(id, form, advertencia, true);
	}

	public CartaoComponent(String id, Form<Component> form, TipoAdvertenciaEnum advertencia, boolean advertido) {
		super(id);
		this.form = form;
		this.advertencia = advertencia;
		this.advertido = advertido;
		setOutputMarkupId(true);
		montarComponente();
	}

	public CartaoComponent(String id, TipoAdvertenciaEnum advertencia, boolean select) {
		this(id, null, advertencia, select);
	}

	private void changeOnClickCartao(AjaxRequestTarget target){
		WebMarkupContainer cartao = (WebMarkupContainer) CartaoComponent.this.get(CARTAO_ID);
		cartao.setOutputMarkupId(true);
		if(advertido){
			advertido = false;
		}else{
			advertido = true;
		}
		cartao.add(getStyleCartao());
		target.add(cartao);
	}

	public void changeSelectCartao(AjaxRequestTarget target, boolean advertido){
		setAdvertido(advertido);
		WebMarkupContainer cartao = (WebMarkupContainer) CartaoComponent.this.get(CARTAO_ID);
		cartao.setOutputMarkupId(true);
		cartao.add(getStyleCartao());
		target.add(cartao);
	}

	private WebMarkupContainer criarCartao(boolean select){
		WebMarkupContainer cartao = new WebMarkupContainer(CARTAO_ID);

		if(getForm() != null) {
			if(select) {
				cartao.add(new AjaxFormSubmitBehavior(getForm(), JavaScriptParam.ON_CLICK){
					private static final long serialVersionUID = 3251885340337521288L;
					@Override
					protected void onError(AjaxRequestTarget target) {
					}
					@Override
					protected void onSubmit(AjaxRequestTarget target) {
						changeOnClickCartao(target);
					}
				});

			}
		}
		cartao.add(getStyleCartao());

		return cartao;
	}

	public TipoAdvertenciaEnum getAdvertencia() {
		return advertencia;
	}

	public Form<Component> getForm() {
		return form;
	}

	private SimpleAttributeModifier getStyleCartao(){
		StringBuilder styleString = new StringBuilder();
		styleString.append(STYLE_CARTAO);
		styleString.append("background-color:");
		styleString.append(ColorAdvertencia.getColorTipo(advertido, getAdvertencia()));
		SimpleAttributeModifier style = new SimpleAttributeModifier(HtmlParam.STYLE, styleString.toString());
		return style;
	}

	public boolean isAdvertido() {
		return advertido;
	}

	@Override
	public void montarComponente() {
		add(criarCartao(isAdvertido()));
	}

	public void setAdvertencia(TipoAdvertenciaEnum advertencia) {
		this.advertencia = advertencia;
	}

	public void setAdvertido(boolean advertido) {
		this.advertido = advertido;
	}

	public void setForm(Form<Component> form) {
		this.form = form;
	}

}
